package chenji.gunapi.gun;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.inventory.ItemStack;

import chenji.gunapi.untl.asm.AsmManager;

public class GunManager {
	private final Map<String, Gun> gunlist = new HashMap<>();

	public boolean addGun(Gun gun) {
		if (this.gunlist.containsKey(gun.getName())) {
			return false;
		}
		this.gunlist.put(gun.getName(), gun);
		return true;
	}

	public Gun getGun(String name) {
		return this.gunlist.get(name);
	}

	public boolean removeGun(Gun gun) {
		if (this.gunlist.containsKey(gun.getName())) {
			this.gunlist.remove(gun.getName());
			return true;
		}
		return false;
	}

	public String getGunName(ItemStack item) {
		String s = AsmManager.getAsmManager().getNBTManager().getNBTTagFromItem(item, "gunname");
		if (s == null) {
			return null;
		}
		return s;
	}

	public Gun getGun(ItemStack item) {
		String s = this.getGunName(item);
		if (s != null) {
			return this.getGun(s);

		}
		return null;
	}

	public ItemStack writeNBT(ItemStack item, String name) {
		return AsmManager.getAsmManager().getNBTManager().setNBTTagFromItem(item, "gunname", name);
	}

	public ItemStack getFinalItem(Gun g) {
		if (g.getItemStack() != null) {
			return this.writeNBT(g.getItemStack(), g.getName());
		}
		return null;
	}
}
